//
//  DealDetailViewController.m
//  BaiTuan
//
//  Created by huiwenjiaoyu on 16/1/4.
//  Copyright © 2016年 YR. All rights reserved.
//

#import "DealDetailViewController.h"
#import "UIImageView+AFNetworking.h"


@interface DealDetailViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *iconImage;
@property (nonatomic, strong) UILabel *descriptionLable;
@property (nonatomic, strong) NSDictionary *info;
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation DealDetailViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    [super viewDidLoad];
    
    [self createView];
    [self webView];
}

#pragma mark - 页面加载

- (UIWebView *)webView
{
    if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200, kScreenWidth, kScreenHeight - 200 - 64)];
        _webView.scrollView.delegate = self;
        _webView.scalesPageToFit = YES;
        [self.view insertSubview:_webView atIndex:0];
    }
    return _webView;
}

- (void)createView
{
    if (self.iconImage == nil) {
        self.iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        self.iconImage.image = [UIImage imageNamed:@"bg_merchant_photo_placeholder_big"];
        [self.view addSubview:self.iconImage];
        
        self.descriptionLable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.iconImage.frame) - 50, kScreenWidth, 50)];
        self.descriptionLable.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        self.descriptionLable.numberOfLines = 0;
        self.descriptionLable.font = [UIFont systemFontOfSize:13];
        self.descriptionLable.textColor = [UIColor whiteColor];
        [self.iconImage addSubview:self.descriptionLable];
    }
}

#pragma mark - 数据更新

- (void)setDeal:(DealModel *)deal
{
    if (_deal != deal) {
        _deal = deal;
        
        self.dealId = self.deal.deal_id;
        [self requestDealDetail];
    }
}

- (void)setDealId:(NSNumber *)dealId
{
    if (_dealId != dealId) {
        _dealId = dealId;
        
        [self requestDealDetail];
    }
}

#pragma mark - 请求数据

- (void)requestDealDetail
{
    
    AFHTTPSessionManager *mananer = [AFHTTPSessionManager manager];
    [mananer.requestSerializer setValue:apikey forHTTPHeaderField:@"apikey"];
    
    
    [mananer GET:deal_detail
      parameters:@{
                   @"deal_id":self.dealId
                   }
        progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             
             if ([responseObject[@"errno"] integerValue] == 0) {
                 
//                 NSLog(@"%@",responseObject[@"deal"]);
                 self.info = responseObject[@"deal"];
                 [self.iconImage setImageWithURL:[NSURL URLWithString:self.info[@"image"]]placeholderImage:[UIImage imageNamed:@"bg_merchant_photo_placeholder_big"]];
                 [self.descriptionLable setText:self.info[@"long_title"]];
                 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.info[@"deal_murl"]]]];
                 
                 //                 NSString *HTMLString = [NSString stringWithFormat:@"%@\n%@",self.info[@"buy_contents"],self.info[@"consumer_tips"]];
                 //                 [self.webView loadHTMLString:HTMLString baseURL:nil];
                 [self loadWebView];
                 
             }else {
                 
                 UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络加载错误,重新加载" preferredStyle:UIAlertControllerStyleAlert];
                 UIAlertAction *reload = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                     
                 }];
                 
                 [alter addAction:reload];
                 [self presentViewController:alter animated:YES completion:nil];
             }
             
             
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             
             
         }];
}

#pragma mark - 加载页面

- (void)loadWebView
{
    NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"news.html" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
    NSString *images = self.info[@"buy_details"];
    [self.webView loadHTMLString:[NSString stringWithFormat:html,self.info[@"buy_contents"],images,self.info[@"consumer_tips"]] baseURL:nil];
}

#pragma mark - scorllview Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    CGFloat hight = self.iconImage.frame.size.height;
//    
//    if (scrollView.contentOffset.y <= 0) {
//        
//        CGFloat scale = (hight - scrollView.contentOffset.y) / hight;
//        if (scale <= 1) {
//            scale = 1;
//        }
//        CGRect frame = self.iconImage.frame;
//        frame.size.height = hight - scrollView.contentOffset.y;
//        self.iconImage.frame = frame;
//    }
}

@end
